package org.nanotek.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

import org.nanotek.beans.ArtistCredit;
import org.nanotek.beans.ArtistCreditName;
import org.nanotek.beans.ArtistName;
import org.nanotek.dao.ArtistCreditDAO;
import org.nanotek.dao.ArtistCreditNameDAO;
import org.nanotek.dao.ArtistDAO;
import org.nanotek.dao.mapper.ArtistMapper;
import org.nanotek.dao.mapper.ArtistNameMapMapper;
import org.nanotek.service.ArtistService;

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@SuppressWarnings("unchecked")
public class ArtistServiceImpl implements ArtistService {

	@EJB 
	ArtistDAO<ArtistName> artistDAO;
	
	@EJB 
	ArtistCreditDAO<ArtistCredit> artistCreditDAO; 
	
	@EJB
	ArtistCreditNameDAO<ArtistCreditName> artistCreditNameDAO;
	
	@Override
	public ArtistName findArtistById(Long code) {
		return artistDAO.find(code);
	}

	@Override
	public <T extends ArtistName> List<T> loadArtistList(Integer startRecord, Integer numRecords) {
		return artistDAO.listRecords(startRecord, numRecords).stream().map(new ArtistMapper<T>()).collect(Collectors.toList());
	}
	
	public ArtistCredit findArtistCredit (Long id) 
	{ 
		ArtistCredit artistCredit = null;
		ArtistCreditName aName = artistCreditNameDAO.findArtistCreditByMbid(id);
		if (aName !=null){ 
			/*artistCredit = new ArtistCredit();
			artistCredit.setArtistCreditId(aName.getArtistCredit());*/
			artistCredit = artistCreditDAO.findArtistCreditByArtistCreditId(aName.getArtistCredit());
		}
		return artistCredit;
	}

	@Override
	public Set<ArtistCredit> findArtistCredits(Long artistId) {
		return artistDAO.findArtistCredits(artistId);
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, ?>> prepareReportResult(List<?> artists) {
		List<ArtistName> innerList = (List<ArtistName>) artists;
		return innerList.stream().map(new ArtistNameMapMapper()).collect(Collectors.toList());
	}
//(Function<?, ? extends R>) new ArtistNameHashMapper()).collect(Collectors.toList());

	@Override
	public List<?> findArtistByName(String value) {
		return artistDAO.findByName(value, true);
	}
}
